# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/4/24 11:27
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : layer_8_evaluate_solution.py
# @Version: OpenPlanner2.0
import importlib

from OpenPlanner.core.src.op_class import OPClass
from OpenPlanner.debug.debug_utils import DEBUG_PRINT


class SolutionEvaluatingModule:
    def __init__(self,
                 op_obj: OPClass):
        self.op_obj = op_obj
        self.solution_evaluate_hook_func = None

    def evaluate_solution(self):
        if self.op_obj.solution_evaluation_option.latency_eval_flag:
            pass

        if self.op_obj.solution_evaluation_option.jitter_eval_flag:
            pass

        if self.op_obj.solution_evaluation_option.porosity_eval_flag:
            pass

    # 钩子函数相关
    def _register_evaluate_hook(self,
                                func):
        self.solution_evaluate_hook_func = func

    def custom_evaluating_hook(self):
        base_module = \
            self.op_obj.quality_eval_hook.base_module_path
        func_name = \
            self.op_obj.quality_eval_hook.hook_func_name

        if base_module != '' and func_name != '':
            # 注册钩子
            # 判断模块是否存在
            try:
                module = importlib.import_module(base_module)
            except ModuleNotFoundError:
                print("ModuleNotFoundError:"
                      "质量评估钩子函数的%s模块不存在，"
                      "请检查钩子函数路径后更新配置文件" % base_module)
                exit(0)
            try:
                func = getattr(module, func_name)
            except AttributeError:
                print("AttributeError:"
                      "模块%s内没有函数%s，"
                      "请检查质量评估钩子函数名称（quality_eval_hook"
                      "->hook_func_name）"
                      "后更新配置文件"
                      % (base_module, func_name))
                exit(0)
            self._register_evaluate_hook(func)

        # 执行钩子函数
        if self.solution_evaluate_hook_func is not None:
            kwargs = self.op_obj.input_init_hook.hook_func_param
            self.solution_evaluate_hook_func(self.op_obj, **kwargs)
        else:
            DEBUG_PRINT("No hook after solution evaluating")
            pass
